package com.rmt.www.daoImpl;

import java.util.List;

import javax.sql.DataSource;

import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.jdbc.core.JdbcTemplate;

import com.rmt.www.constant.ArtifactType;
import com.rmt.www.dao.ArtifactDao;
import com.rmt.www.dao.TestArtifactDao;
import com.rmt.www.vo.Artifact;
import com.rmt.www.vo.Project;
import com.rmt.www.vo.TestArtifact;

public class TestArtifactDaoImpl implements TestArtifactDao {
	
	private JdbcTemplate jdbcTemplate;

	public void setDataSource(DataSource dataSource) {
		this.jdbcTemplate = new JdbcTemplate(dataSource);
	}

	public int insertTestArtifact(TestArtifact artifact) {
		jdbcTemplate.update("insert into testartifact(pid,testcasetype,title,priority,precondition,input,testdata,steps,expectedresult,postcondition,effort,cost,risk,comment,createrid,createdtime,updaterid,updatedtime,updatedtimes,version,status) " +
							"values(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)", 
					new Object[]{
						artifact.getPid(),
						artifact.getTestcasetype(),
						artifact.getTitle(),
						artifact.getPriority(),
						artifact.getPrecondition(),
						artifact.getInput(),
						artifact.getTestdata(),
						artifact.getSteps(),
						artifact.getExpectedresult(),
						artifact.getPostcondition(),
						artifact.getEffort(),
						artifact.getCost(),
						artifact.getRisk(),
						artifact.getComment(),
						artifact.getCreaterid(),
						artifact.getCreatedtime(),
						artifact.getUpdaterid(),
						artifact.getUpdatedtime(),
						artifact.getUpdatedtimes(),
						artifact.getVersion(),
						artifact.getStatus()
					},new int[] {
						java.sql.Types.INTEGER,
						java.sql.Types.VARCHAR,
						java.sql.Types.VARCHAR,
						java.sql.Types.VARCHAR,
						java.sql.Types.VARCHAR,
						java.sql.Types.VARCHAR,
						java.sql.Types.VARCHAR,
						java.sql.Types.VARCHAR,
						java.sql.Types.VARCHAR,
						java.sql.Types.VARCHAR,
						java.sql.Types.INTEGER,
						java.sql.Types.VARCHAR,
						java.sql.Types.VARCHAR,
						java.sql.Types.VARCHAR,
						java.sql.Types.INTEGER,
						java.sql.Types.TIMESTAMP,
						java.sql.Types.INTEGER,
						java.sql.Types.TIMESTAMP,
						java.sql.Types.INTEGER,
						java.sql.Types.VARCHAR,
						java.sql.Types.VARCHAR
					});
		int lastInsertId = jdbcTemplate.queryForInt("select last_insert_id()");
		return lastInsertId;
	}

	public void updateTestArtifact(TestArtifact artifact) {
		jdbcTemplate.update("update testartifact " +
							"set pid=?,testcasetype=?,title=?,priority=?,precondition=?,input=?,testdata=?,steps=?,expectedresult=?,postcondition=?,effort=?,cost=?,risk=?,comment=?,createrid=?,createdtime=?,updaterid=?," +
							"updatedtime=?,updatedtimes=?,version=?,status=?" +
							"where id=?", 
							new Object[]{
								artifact.getPid(),
								artifact.getTestcasetype(),
								artifact.getTitle(),
								artifact.getPriority(),
								artifact.getPrecondition(),
								artifact.getInput(),
								artifact.getTestdata(),
								artifact.getSteps(),
								artifact.getExpectedresult(),
								artifact.getPostcondition(),
								artifact.getEffort(),
								artifact.getCost(),
								artifact.getRisk(),
								artifact.getComment(),
								artifact.getCreaterid(),
								artifact.getCreatedtime(),
								artifact.getUpdaterid(),
								artifact.getUpdatedtime(),
								artifact.getUpdatedtimes(),
								artifact.getVersion(),
								artifact.getStatus(),
								artifact.getId()
						}, new int[] {
								java.sql.Types.INTEGER,
								java.sql.Types.VARCHAR,
								java.sql.Types.VARCHAR,
								java.sql.Types.VARCHAR,
								java.sql.Types.VARCHAR,
								java.sql.Types.VARCHAR,
								java.sql.Types.VARCHAR,
								java.sql.Types.VARCHAR,
								java.sql.Types.VARCHAR,
								java.sql.Types.VARCHAR,
								java.sql.Types.INTEGER,
								java.sql.Types.VARCHAR,
								java.sql.Types.VARCHAR,
								java.sql.Types.VARCHAR,
								java.sql.Types.INTEGER,
								java.sql.Types.TIMESTAMP,
								java.sql.Types.INTEGER,
								java.sql.Types.TIMESTAMP,
								java.sql.Types.INTEGER,
								java.sql.Types.VARCHAR,
								java.sql.Types.VARCHAR,
								java.sql.Types.INTEGER
						});
	}

	
	public void deleteTestArtifactById(int id) {
		jdbcTemplate.update("delete from testartifact where id=?", new Object[]{id}, new int[]{java.sql.Types.INTEGER});
	}

	public TestArtifact getTestArtifactById(int id) {
		try {	
			return  (TestArtifact) this.jdbcTemplate.queryForObject("select * from testartifact where id = ?",
					new Object[]{id}, new TestArtifactRowMapper());
		  }catch (final EmptyResultDataAccessException e) {
			  return null;
		  }
	}

	@SuppressWarnings("unchecked")
	public List<TestArtifact> getTestArtifactsByPId(int pid) {
		return (List<TestArtifact>)(this.jdbcTemplate.query("select * from testartifact where pid=?",
				new Object[]{pid}, 
				new int[]{java.sql.Types.INTEGER}, new TestArtifactRowMapper()));
	}
}
